/* written by Jonathan Odom and Aneesh Butani
 * 
 * Some of the string literals have been left in this code,
 * simply because this parser class will be completely redesigned
 * if not replaced within the next few days.  For the final
 * submission, the code will meet all of the required c++ conventions.
 */

#include "XMLParser.h"
#include "Appt.h"


string XMLParser::APPT = "appt";
string XMLParser::DAY = "day";
string XMLParser::TIME = "time";
string XMLParser::SUBJECT = "subject";

XMLParser::XMLParser()
{
}

XMLParser::~XMLParser()
{
}

XMLParser::XMLParser(const string& fileName)
: myFileName(fileName)
{
}

XMLParser::XMLParser(const string& fileName, int whichEvent)
: myFileName(fileName)
{
	if(whichEvent == 1)
	{
		readAppt();
	}
	else if(whichEvent == 2)
	{
		readBirthday();
	}
	else XMLParser(fileName);
	
}

void XMLParser::readAppt()
{
	//Maybe make a readNodeAppt method?
	//then just make a new readNode for each input
	//Something better then if statements in read()?
	
	//Why is this not working ???
	ifstream f(myFileName.c_str());
	string node;
	string temp;
	while(!f.eof())
	{
		//getline does not include delimiter
		getline(f,temp, '<');
		getline(f,node, '>');
		if(node.at(1)!='!')
		{
			readNodeAppt(node);
		}
		
	}
	
}

void XMLParser::readNodeAppt(const string& node)
{
	string tempDay = getInfoAppt(node,DAY);
	string tempSubject = getInfoAppt(node,SUBJECT);
	int range[2]; 
	getTimeRangeAppt(node, TIME, range);
	Appt cur(atoi(tempDay.c_str()),range[0], range[1],tempSubject);
	myEvents.push_back(cur);
	//cout<<range[0]<<" to "<<range[1]<<endl;
	
}

string XMLParser::getInfoAppt(const string& node, const string& infoType)
{
	unsigned int loc = node.find(infoType,0);
	if(loc != string::npos)
	{
		int start = node.find('\"', loc);
		int end = node.find('\"', start+1);
		return(node.substr(start+1,end-start-1));
	}
	return "Type not found";
}

void XMLParser::getTimeRangeAppt(const string& node, const string& infoType, int range[])
{
	range[0] = 0;
	range[1] = 0;

	unsigned int loc = node.find(infoType,0);
	if(loc != string::npos)
	{
		int start = node.find("\"",loc);
		int end = node.find("\"", start + 1);
		seperateTime(range, node.substr(start+1,end-start-1));
		
	}
}

void XMLParser::seperateTime(int range[], const string& times)
{
	unsigned int loc = times.find(".",0);
	unsigned int loc2 = times.find(",",0);
	if(loc != string::npos)
	{
		string temp = times.substr(0, loc);
		range[0]= atoi(temp.c_str());
		temp = times.substr(loc+1,times.length());
		range[1] = atoi(temp.c_str());
	}
	else if(loc2 != string::npos)
	{
		string temp = times.substr(0, loc2-1);
		range[0]= atoi(temp.c_str());
		temp = times.substr(loc2+1,times.length());
		range[1] = atoi(temp.c_str());
	}
		//cout<<range[0]<<" to "<<range[1]<<endl;
}

//Change
vector<Event> XMLParser::getEvents()
{
	return myEvents;
}

/*vector<Event> XMLParser::getEvents(const string& filter)
{
	if (filter=="")
		return getEvents();
	else {
		vector<Event> filteredEvents;
		for(unsigned int i = 0; i<myEvents.size(); i++)
			if(myEvents[i].getDescription()==filter)
				filteredEvents.push_back(myEvents[i]);
		return filteredEvents;
	}
}*/


//methods for birthday, we know this should be done through inheritance,
//but the structure would not make sense so we are doing this and
//will refactor later into an organized structure

void XMLParser::readBirthday()
{
	APPT = "EVENT";
	DAY = "DATE";
	SUBJECT = "DESCRIPTION";
	ifstream f(myFileName.c_str());
	string node;
	string temp;
	while(!f.eof())
	{
		//getline does not include delimiter
		getline(f,temp, '<');
		getline(f,node, '>');
		if(node.at(1)!='!')
		{
			readNodeBirthday(node);
		}
		
	}
	
}

void XMLParser::readNodeBirthday(const string& node)
{
	string tempDay = getInfoAppt(node,DAY);
	string tempSubject = getInfoAppt(node,SUBJECT);
	Event cur(atoi(tempDay.c_str()), tempSubject);
	myEvents.push_back(cur);
	
}
